package com.example.springboot.Controller;

import com.example.springboot.Lib.ResponseEntity;
import com.example.springboot.Service.UserService;
import com.example.springboot.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 获取用户列表
    @PostMapping("/getList")
    public ResponseEntity getUserList(){
        try{
            List<Users> res = userService.getList();
            if (res != null && !res.isEmpty()){
                return ResponseEntity.ResponseApi(200,"success",res);
            } else if (res != null && res.isEmpty()) {
                return ResponseEntity.ResponseApi(201,"暂无用户",res);
            } else {
                return ResponseEntity.ResponseApi(401,"查询失败",null);
            }
        } catch (Exception exception){
            return ResponseEntity.ResponseApi(400,"failed",exception);
        }
    }

    //注册
    @PostMapping("/register")
    public ResponseEntity register(@RequestBody @Validated Users user){
        try{
            Date time = new Date();
            Integer res = userService.register(user.getUser_id(), user.getUser_name(), user.getPassword(), user.getAddress(), user.getTel(), null, null, time, user.getRole());

            if (res != 0){
                return ResponseEntity.ResponseApi(200,"success",null);
            } else {
                return ResponseEntity.ResponseApi(401,"failed",null);
            }
        } catch (Exception exception){
            return ResponseEntity.ResponseApi(400,"failed",exception);
        }
    }

    // // 登录
    // @PostMapping("/login")
    // public ResponseEntity login(@RequestParam("username") String username)
}
